setClass("kPCAReducer",contains="vPCAReducer",
		representation(
				.kernel="character",
				.kpar="list"
				),
		prototype=prototype(
				.description="kernel PCA Reducer Class",
				.kernel="laplacedot",
				.kpar=list(sigma = 0.1)
		)
		
)

#methods
#TODO set initialize method (include kpca parameters) call base constructor
## S4 method for signature 'matrix'
#kpca(x, kernel = "rbfdot", kpar = list(sigma = 0.1), features = 0,
#		th = 1e-4, na.action = na.omit, ...)

#reatures is equall to dimension number in oryginal space
#
setMethod("initialize",
		signature="kPCAReducer",
		function(.Object,dimChooser,kernel="anovadot",kpar=list(sigma = 0.1),...){
			.Object <- callNextMethod(.Object,dimChooser);
			.Object@.kernel <- kernel;
			.Object@.kpar <- kpar;
			return(.Object)
		})
setMethod("reduce",
		signature="kPCAReducer",
		definition=function(.Object,inputSet,...){

			inputSet <- as.data.frame(inputSet)
			features <- min( {dim(inputSet)[1]-1 },dim(inputSet)[2])
			#estimate sigma using  Thorstensen method gaussian kernel
			quants <-quantile(dist(inputSet))
			quants <- quants[quants>0]
			.Object@.kpar=list(sigma = quants[which.min(quants)], degree=5 )
			#.Object@.kpar=list(sigma = quantile(dist(inputSet))[1] )
			#.Object@.kpar=list(offset=1,scale=0.5)
			
			
			tmpResult <-kernlab::kpca(as.matrix(inputSet),kernel=.Object@.kernel,kpar=.Object@.kpar,features=features)
			
			.Object@.eigenValues	<- tmpResult@eig;
			.Object@.eigenVectors	<-tmpResult@pcv;
			.Object@.rotatedSet		<-tmpResult@rotated;
			.Object@.tmpData		<-as.matrix(inputSet)
			
			dim <-chooseBest(.Object@.dimChooser,.Object)
			
			.Object@.rotatedSet <- (.Object@.rotatedSet)[,1:dim,drop=FALSE] 
			.Object@.resEnv$savedSet <-.Object@.rotatedSet
			#.Object@.tmpData		<-matrix()
			return(.Object@.rotatedSet)
		})
			
